package com.ruoyi.web.controller.kpl.other;

import okhttp3.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class DebugLonghuRequest {
    public static void main(String[] args) throws IOException {
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url("https://apphq.longhuvip.com/w1/api/index.php?Order=1&a=RealRankingInfo&st=30&apiv=w21&Type=1&c=ZhiShuRanking&PhoneOSNew=1&ZSType=7")
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
                .addHeader("Accept", "*/*")
                // 移除可能引起问题的编码头
                // .addHeader("Accept-Encoding", "gzip")
                .build();

        try (Response response = client.newCall(request).execute()) {
            System.out.println("响应码: " + response.code());
            System.out.println("Content-Type: " + response.header("Content-Type"));
            System.out.println("Content-Encoding: " + response.header("Content-Encoding"));

            if (response.isSuccessful()) {
                // 方法1: 直接获取字节并转换为字符串
                byte[] bytes = response.body().bytes();
                String body1 = new String(bytes, StandardCharsets.UTF_8);
                System.out.println("方法1 (UTF-8): " + body1);

                // 方法2: 尝试GBK编码
                String body2 = new String(bytes, "GBK");
                System.out.println("方法2 (GBK): " + body2);

                // 方法3: 使用body().string()
                // 需要重新创建response，因为body().bytes()已经消费了body
                // String body3 = response.body().string();
                // System.out.println("方法3 (string()): " + body3);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}